package cn.beausoft.mybatis3.test.dao.auto.impl;

import cn.beausoft.mybatis3.sqlbuild.Handle;
import cn.beausoft.mybatis3.sqlbuild.condition.ManyCondition;
import cn.beausoft.mybatis3.sqlbuild.condition.original.ICondition;
import cn.beausoft.mybatis3.sqlbuild.selector.SelectMetadata;
import cn.beausoft.mybatis3.sqlbuild.selector.SelectResolver;
import cn.beausoft.mybatis3.sqlbuild.selector.Selector;
import com.github.miemiedev.mybatis.paginator.domain.PageBounds;
import com.github.miemiedev.mybatis.paginator.domain.PageList;
import cn.beausoft.mybatis3.test.entity.FileResource;
import cn.beausoft.mybatis3.test.dao.auto.FileResourceAutoDao;
import cn.beausoft.mybatis3.test.mapper.auto.FileResourceAutoMapper;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
* 文件资源表
* @author hjp
**/
@Component
@SuppressWarnings({ "unchecked", "rawtypes" })
public class FileResourceAutoDaoImpl implements FileResourceAutoDao {
    @Resource(name="fileResourceAutoMapper")
    private FileResourceAutoMapper mapper;

    @Override
    public List<FileResource> selectAll() {
        return this.mapper.selectAll();
    }

    /**
     * 根据条件查询
     *
     * @param conditions
     * @return
     */
    public List<FileResource> select(ICondition... conditions) {
        ManyCondition manyCondition = new ManyCondition();
        for (int i = 0; i < conditions.length; i++) {
            manyCondition.add(conditions[i]);
        }
        return this.mapper.select(new SelectResolver(manyCondition, this.TABLE, this.ALL_FIELDS).toMetadata());
    }

    @Override
    public List<FileResource> select(Selector<FileResource> selector) {
        SelectResolver resolver = new SelectResolver(selector, this.TABLE, this.ALL_FIELDS);
        SelectMetadata metadata = resolver.toMetadata();
        return this.mapper.select(metadata);
    }

    @Override
    public List<FileResource> select(FileResource bean) {
        ManyCondition conditions = this.SELECT_WHERE_HANDLE.handle(bean);
        SelectResolver resolver = new SelectResolver(conditions, this.TABLE, this.ALL_FIELDS);
        SelectMetadata metadata = resolver.toMetadata();
        return this.mapper.select(metadata);
    }

    @Override
    public FileResource selectRow(ICondition... conditions) {
        ManyCondition manyCondition = new ManyCondition();
        for (int i = 0; i < conditions.length; i++) {
            manyCondition.add(conditions[i]);
        }
        return this.mapper.selectRow(new SelectResolver(manyCondition, this.TABLE, this.ALL_FIELDS).toMetadata());
    }

    @Override
    public FileResource selectRow(Selector<FileResource> selector) {
        SelectResolver resolver = new SelectResolver(selector, this.TABLE, this.ALL_FIELDS);
        SelectMetadata metadata = resolver.toMetadata();
        return this.mapper.selectRow(metadata);
    }

    @Override
    public FileResource selectRow(FileResource bean) {
        ManyCondition conditions = this.SELECT_WHERE_HANDLE.handle(bean);
        SelectResolver resolver = new SelectResolver(conditions, this.TABLE, this.ALL_FIELDS);
        SelectMetadata metadata = resolver.toMetadata();
        return this.mapper.selectRow(metadata);
    }
    
    @Override
    public FileResource selectByResourceId(Integer resourceId) {
        return selectRow(this.resourceId.toEqCondition(resourceId));
    }

    @Override
    public <Output> List<Output> select(Selector<FileResource> selector, Handle<Map, Output> handle) {
        SelectResolver resolver = new SelectResolver(selector, this.TABLE, this.ALL_FIELDS);
        List maps = this.mapper.selectForListMap(resolver.toMetadata());
        for (int i = 0; i < maps.size(); i++) {
            Output out = handle.handle((Map) maps.get(i));
            maps.set(i, out);
        }
        return maps;
    }

    @Override
    public <Output> PageList<Output> select(Selector<FileResource> selector, Handle<Map, Output> handle, PageBounds bounds) {
        SelectResolver resolver = new SelectResolver(selector, this.TABLE, this.ALL_FIELDS);
        PageList maps = this.mapper.selectPageForListMap(resolver.toMetadata(), bounds);
        for (int i = 0; i < maps.size(); i++) {
            Object out = handle.handle((Map) maps.get(i));
            maps.set(i, out);
        }
        return maps;
    }

    @Override
    public <Output> Output selectRow(Selector<FileResource> selector, Handle<Map, Output> handle) {
        SelectResolver resolver = new SelectResolver(selector, this.TABLE, this.ALL_FIELDS);
        Map map = this.mapper.selectForMap(resolver.toMetadata());
        return handle.handle(map);
    }


    @Override
    public Long selectCount(ICondition... condition) {
        Selector<FileResource> selector = new Selector<>();
        selector.addStringField(this.COUNT_FIELD);
        selector.where(condition);
        SelectResolver resolver = new SelectResolver(selector, this.TABLE, this.ALL_FIELDS);
        List list = this.mapper.selectForListMap(resolver.toMetadata());
        if (list.size() > 0) {
            return this.COUNT_HANDLE.handle((Map) list.get(0));
        }
        return 0l;
    }

    @Override
    public Long selectCount(FileResource bean) {
        ManyCondition conditions = this.SELECT_WHERE_HANDLE.handle(bean);
        return selectCount(conditions);
    }

    @Override
    public PageList<FileResource> select(PageBounds page, ICondition... condition) {
        Selector<FileResource> selector = new Selector<>();
        selector.where(condition);
        return select(selector, page);
    }

    @Override
    public PageList<FileResource> select(Selector<FileResource> selector, PageBounds page) {
        SelectResolver resolver = new SelectResolver(selector, this.TABLE, this.ALL_FIELDS);
        return mapper.selectPage(resolver.toMetadata(), page);
    }

    @Override
    public PageList<FileResource> select(FileResource bean, PageBounds page) {
        SelectResolver resolver = new SelectResolver(this.SELECT_WHERE_HANDLE.handle(bean), this.TABLE, this.ALL_FIELDS);
        return mapper.selectPage(resolver.toMetadata(), page);
    }
    //endregion

    //region write data
    @Override
    public int insert(FileResource bean) {
        return this.mapper.insert(bean);
    }

    @Override
    public int insertSelective(FileResource bean) {
        return this.mapper.insertSelective(bean);
    }

    @Override
    public int updateByResourceId(FileResource bean) {
        return this.mapper.update(bean, this.resourceId.toEqCondition(bean.getResourceId()).prepare());
    }

    @Override
    public int updateSelectiveByResourceId(FileResource bean) {
        return this.mapper.updateSelective(bean, this.resourceId.toEqCondition(bean.getResourceId()).prepare());
    }


    @Override
    public int deleteByResourceId(Integer resourceId) {
        return this.mapper.delete(this.resourceId.toEqCondition(resourceId).prepare());
    }


    @Override
    public int update(FileResource bean, ICondition... condition) {
        ManyCondition conditions = new ManyCondition();
        for (int i = 0; i < condition.length; i++) {
            conditions.add(condition[i]);
        }
        return this.mapper.update(bean, conditions.prepare());
    }

    @Override
    public int updateSelective(FileResource bean, ICondition... condition) {
        ManyCondition conditions = new ManyCondition();
        for (int i = 0; i < condition.length; i++) {
            conditions.add(condition[i]);
        }
        return this.mapper.updateSelective(bean, conditions.prepare());
    }

    @Override
    public int delete(ICondition... condition) {
        ManyCondition conditions = new ManyCondition();
        for (int i = 0; i < condition.length; i++) {
            conditions.add(condition[i]);
        }
        return this.mapper.delete(conditions.prepare());
    }
    //endregion
}